﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace black_screen_mode_server.Models
{
    /// <summary>
    /// MD5加密字符串
    /// </summary>
    public class MD5Encode
    {
        /// <summary>
        /// MD5加密字符串
        /// </summary>
        /// <param name="ConvertString"></param>
        /// <returns></returns>       
        public string GetMd5Str(String ConvertString)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            DES des = new DESCryptoServiceProvider();
            byte[] res = md5.ComputeHash(Encoding.Default.GetBytes(ConvertString), 0, ConvertString.Length);
            String returnThis = "";
            for (int i = 0; i < res.Length; i++)
            {
                returnThis += System.Uri.HexEscape((char)res[i]);
            }
            returnThis = returnThis.Replace("%", "");
            returnThis = returnThis.ToLower();
            return returnThis;
        }

        public static string MD5(string Text, string charset)
        {
            byte[] buffer = System.Text.Encoding.GetEncoding(charset).GetBytes(Text);
            try
            {
                System.Security.Cryptography.MD5CryptoServiceProvider check;
                check = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] somme = check.ComputeHash(buffer);
                string ret = "";
                foreach (byte a in somme)
                {
                    if (a < 32)
                        ret += "0" + a.ToString("X");
                    else
                        ret += a.ToString("X");
                }
                return ret.ToLower();
            }
            catch
            {
                throw;
            }
        }
        private static string base64(String str, String charset)
        {
            return Convert.ToBase64String(System.Text.Encoding.GetEncoding(charset).GetBytes(str));
        }



    }
}
